﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using System.IO;
using System.Xml;

namespace NFAPI
{
    public static class CatalogDal
    {
        #region Public Methods
        public static CatalogList SearchTitles(INetflixService service, string term, int startIndex, int maxResults)
        {
            return (CatalogList)SerializationHelper.FromXml(typeof(CatalogList), service.SearchTitles(term, startIndex, maxResults));
        }

        public static CatalogTitle GetTitleDetails(INetflixService service, Item item)
        {
            return (CatalogTitle)SerializationHelper.FromXml(typeof(CatalogTitle), service.GetTitleDetails(item.TitleRef));
        }

        public static CatalogList GetSimilars(INetflixService service, Item item, int startIndex, int maxResults)
        {
            return (CatalogList)SerializationHelper.FromXml(typeof(CatalogList), service.GetSimilars(item.Similars.Href, startIndex, maxResults));
        }

        public static string GetSynopsis(INetflixService service, Item title)
        {
            if (title.Synopsis == null) return null;
            return GetGenericResource(service, title.Synopsis.Href, "/synopsis/text()");
        }

        public static List<Person> GetCast(INetflixService service, Item title)
        {
            return GetPersonList(service, title.Cast);
        }

        public static List<Person> GetDirectors(INetflixService service, Item title)
        {
            return GetPersonList(service, title.Directors);
        }

        public static List<FormatAvailability> GetFormats(INetflixService service, Item title)
        {
            if (title.Formats == null) return null;
            return (List<FormatAvailability>)SerializationHelper.FromXml(typeof(FormatAvailabilityList), service.GetGenericResource(title.Formats.Href));
        }

        public static Filmography GetFilmography(INetflixService service, Person person)
        {
            if (person.Filmography == null) return null;
            return (Filmography)SerializationHelper.FromXml(typeof(Filmography), service.GetGenericResource(person.Filmography.Href));
        }

        public static List<Award> GetAwards(INetflixService service, Item title)
        {
            if (title.Awards == null) return null;
            return (List<Award>)SerializationHelper.FromXml(typeof(AwardList), service.GetGenericResource(title.Awards.Href));
        }

        public static Person GetPerson(INetflixService service, Link personLink)
        {
            return (Person)SerializationHelper.FromXml(typeof(Person), service.GetGenericResource(personLink.Href));
        }
        #endregion

        #region Private Methods
        private static List<Person> GetPersonList(INetflixService service, Link link)
        {
            if (link == null) return null;
            return (List<Person>)SerializationHelper.FromXml(typeof(PersonList), service.GetGenericResource(link.Href));
        }

        private static string GetGenericResource(INetflixService service, string uri, string xpath)
        {
            string xml = service.GetGenericResource(uri);
            XPathNavigator doc = new XPathDocument(new StringReader(xml)).CreateNavigator();
            return doc.SelectSingleNode(xpath).Value;
        }
        #endregion
    }
}
